package controller;

import com.alibaba.fastjson.JSON;
import model.GoodsSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsSortService;

import java.util.List;

/**
 * @aythor:侯仪澳
 * @data:2022/7/25
 */
@Controller
@RequestMapping("sort")
public class GoodsSortController {
    @Autowired
    private IGoodsSortService goodsSortService;

    //跳转到分类列表页面
    @RequestMapping("sortPage")
    public ModelAndView sortPage(){
        List<GoodsSort> list = goodsSortService.queryOneSort(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("manager/tree");
        return modelAndView;
    }

    //查询一级分类的所有字段名字
    @RequestMapping(value = "query",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String query(GoodsSort goodsSort){
        List<GoodsSort> list = goodsSortService.queryOneSort(goodsSort);
        JSON json = (JSON) JSON.toJSON(list);//将集合转成JSON
        return json.toJSONString();//将json转成字符串类型的形式
    }

    //跳转到添加分类页面
    @RequestMapping("addSortPage")
    public ModelAndView addSortPage(Integer pId,String pName){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pId",pId);
        modelAndView.addObject("pName",pName);
        modelAndView.setViewName("manager/sort_add");
        return modelAndView;
    }

    //添加一个分类
    @RequestMapping("addGoodsSort")
    @ResponseBody
    public String addGoodsSort(GoodsSort goodsSort){
        return goodsSortService.addGoodsSort(goodsSort);
    }

    //根据ID删除一个分类
    @RequestMapping("delGoodsSort")
    @ResponseBody
    public String delGoodsSort(Integer id){
        return goodsSortService.del(id);
    }
}
